<?php

function formatTweet($rawTweet)
{


	$rawTweet= stripslashes($rawTweet);
	
	$rawTweet = $rawTweet."       "; //just in case we need to "find" a space!
	//echo "original: ".$rawTweet."<br />";
	
	$moduleUrl = "";
	
	$maxLen = strlen($rawTweet);
	
	
	for ($i=0; $i<$maxLen; $i++)
	{
		if ($rawTweet[$i] == '-' && $rawTweet[$i+1] == '-')
		{
	
			//gotcha!
			$cmdPos = strpos($rawTweet, ':', $i);
			$cmdPosSpace = strpos($rawTweet, ' ', $i);

			if (!(bool)$cmdPos)
				$cmdPos = $cmdPosSpace;
			else if ($cmdPosSpace<$cmdPos)
				$cmdPos = $cmdPosSpace; //they forgot the colon. we should still handle it

			$argPos = $cmdPos+1;
	
			$cmdPos-= ($i+2);
			$commandArg = substr($rawTweet, $i+2, $cmdPos);

			$moduleUrl = isKnownCommand($commandArg);

			if (strlen(trim($moduleUrl))<1)
				continue;

			$paramArg = "";
			$endPos = -1;
	
			if ($rawTweet[$argPos]=='"')
			{
				$endPos = strpos($rawTweet, '"', $argPos+1);
				$paramArg = substr($rawTweet, $argPos+1, ($endPos-$argPos-1));
			}
			else
			{
				//need to find the next space			
				$endPos = strpos($rawTweet, ' ', $argPos);
				$paramArg = substr($rawTweet, $argPos, ($endPos-$argPos));
			}

			if ($endPos<0)
				continue;
			$paramArg = trim($paramArg);
			//echo "The Command is: ".$commandArg." with arg: ".$paramArg."<br />";

			//the module url may be a url or a string replacement. test for url
			$output = $moduleUrl;
			if (substr(strtolower($moduleUrl), 0, 7) == "http://")
			{
				$executeModule = $moduleUrl."?param=".urlencode($paramArg);
		
				$output = getModuleOutput($executeModule);
			}
			
	
			if (strcmp(trim($output), "--nopost")==0)
			{
				//something bad happened, and we need to abort.
				$rawTweet = trim($output);
				break;
			}
	
			$replaceString = substr($rawTweet, $i, ($endPos-$i+1));
	
			$rawTweet = str_replace($replaceString, $output, $rawTweet);
			$maxLen = strlen($rawTweet);
			$i=0;
		} 
	}

	return trim($rawTweet);
}
function isKnownCommand($commandArg)
	{
		$moduleUrl = "";
		$bFound = false;		
		require_once("components/_args.php");
		@session_start();
		$access_token = $_SESSION['access_token'];
		$arrCommands = getUserCommands($access_token['user_id'], false);

		foreach($arrCommands as $command)
		{
			if ($command[0] == $commandArg)
			{
				$moduleUrl = $command[8];
				if ($command[7]!=null)
				{
					$moduleUrl = $command[7];
				}
				$bFound = true;
				break;
			}
		}
		
		if (!$bFound)
		{
			$arrCommands = getCommands();
	
			foreach($arrCommands as $command)
			{
				if ($command[0] == $commandArg)
				{
					$moduleUrl = $command[8];
					if ($command[7]!=null)
					{
						$moduleUrl = $command[7];
					}
					$bFound = true;
					break;
				}
			}	
		}
		return $moduleUrl;
	}
function getModuleOutput($fvsWebUrl)
	{

		$ch = curl_init();
		curl_setopt($ch, CURLOPT_URL,$fvsWebUrl);
		curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.1.6) Gecko/20070725 Firefox/2.0.0.6");
		curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);
		curl_setopt($ch, CURLOPT_TIMEOUT, 60);
		@session_start();
		$strCookie = 'PHPSESSID=' . $_COOKIE['PHPSESSID'] . '; path=/';

		curl_setopt($ch, CURLOPT_COOKIE, $strCookie );
		@session_write_close();
		$ret = curl_exec ($ch);

		curl_close($ch);

		return $ret;
	}
?>